﻿using WpfMvvmTemplatePrismHC.Common;
using WpfMvvmTemplatePrismHC.Models.BindingModels;
using WpfMvvmTemplatePrismHC.Models.ProgramModel;
using WpfMvvmTemplatePrismHC.Tools.Events;


namespace WpfMvvmTemplatePrismHC.ViewModels.UserControls
{
    public class ViewAViewModel : BindableBase, IConfirmNavigationRequest
    {
        /// <summary>
        /// ViewAModel 模型
        /// </summary>
        public ViewAModel? ViewAModels { get; set; }

        //容器提供者
        private readonly IContainerProvider? containerProvider;

        //事件聚合器
        public IEventAggregator? eventAggregator;

        public DelegateCommand? OpenDialogCommand { get; set; }
        public DelegateCommand? OpenDialog2Command { get; set; }

        public ViewAViewModel()
        {
            ViewAModels = GlobalStaticData.IsDesignTime ? new ViewAModel() : containerProvider.Resolve<ProgramModel>().ViewAModels;
        }

        public ViewAViewModel(IContainerProvider containerProvider, IEventAggregator eventAggregator)
        {
            ViewAModels = GlobalStaticData.IsDesignTime ? new ViewAModel() : containerProvider.Resolve<ProgramModel>().ViewAModels;

            OpenDialogCommand = new DelegateCommand(OpenDialog);
            OpenDialog2Command = new DelegateCommand(OpenDialog2);
            this.containerProvider = containerProvider;
            this.eventAggregator = eventAggregator;

            //注册消息等待接收
            eventAggregator.GetEvent<ValueChangedEvent<string>>().Subscribe(arg =>
            {
                ViewAModels!.Data = arg.NewValue!;
            });

            //注册包含条件筛选器的消息等待接收，两种方式1.arg =>{}；2. if (arg.Token == "Yes"){}
            eventAggregator.GetEvent<ValueChangedEvent<string>>().Subscribe(arg =>
            {
                ViewAModels!.Data = arg.NewValue!;

                //条件筛选器
                if (arg.Token == "Yes")
                {

                }
            }, ThreadOption.PublisherThread, false, arg =>
            {
                if (arg.NewValue!.Equals("Hello")) return true;
                else return false;
            });

            //注册消息等待接收
            eventAggregator.GetEvent<ValueChangedEvent<int>>().Subscribe(arg =>
            {
                ViewAModels!.Data = arg.NewValue.ToString();
            });
        }

        private void OpenDialog()
        {
            //var dialog = (Application.Current as App)?.Container.Resolve<IDialogService>();

            //打开对话框
            var dialog = containerProvider.Resolve<IDialogService>();

            //传递参数字典
            var param = new DialogParameters
            {
                { "message", ViewAModels?.Data }
            };

            //发布消息
            eventAggregator?.GetEvent<ValueChangedEvent<string>>().Publish(new ValueChangedEventArgs<string> { NewValue = "通过eventAggregator发送的消息" });

            //显示对话框
            dialog.ShowDialog("InquiryDialogView", param, callback =>
            {
                //对话框关闭后的回调
                if (callback.Result != ButtonResult.OK)
                {
                    // 获取传递的参数
                    var dialogParams = callback.Parameters;

                    if (dialogParams.ContainsKey("content"))
                    {
                        var contentValue = dialogParams.GetValue<string>("content");
                        ViewAModels!.Data = contentValue;
                    }
                }
            });
        }


        private void OpenDialog2()
        {
            //var waitDialog = HandyControl.Controls.Dialog.Show(new InquiryDialogView());

            //Thread.Sleep(5000);
            //waitDialog?.Close();
        }

        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            //导航确认是在当前页面导航到其他页面时触发
            bool result = true;
            if (MessageBox.Show("确认导航吗？", "温馨提示：", MessageBoxButton.YesNo) == MessageBoxResult.No)
                result = false;

            continuationCallback(result);
        }

        /// <summary>
        /// 导航进入本页面时触发
        /// </summary>
        /// <param name="navigationContext"></param>
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters.ContainsKey("Message"))
            {
                var message = navigationContext.Parameters["Message"];
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        /// <summary>
        /// 导航离开本页面时触发
        /// </summary>
        /// <param name="navigationContext"></param>
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters.ContainsKey("Message"))
            {
                //两种方式获取传递的参数
                var name = navigationContext.Parameters.GetValue<string>("Message");

                var name2 = navigationContext.Parameters["Message"].ToString();
            }
        }
    }
}